package com.hspedu.socketExercise;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DL5O
 * @version 1.0
 */
public class Server_ {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端，在9999端口,等待客户端连接...");
        Socket accept = serverSocket.accept();//监听客户端，等待客户端连接
        System.out.println("客户端连接成功!");
        InputStream inputStream = accept.getInputStream();//如果客户端没有忘数据通道写入数据，那么就会阻塞到这里
        //需要设置结束标记
        System.out.println("客户端写入数据，成功读取...");

        int readLen = 0;
        byte[] buff = new byte[50];
        while((readLen = inputStream.read(buff))!= -1){
            System.out.println(new String(buff,0,readLen));
        }
        accept.shutdownInput();//设置一个结束标记

        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("hello,client".getBytes());
        accept.shutdownOutput();//设置一个结束标记

        outputStream.close();
        inputStream.close();
        accept.close();
        System.out.println("服务端退出...");
    }
}
